package com.ruoyi.web.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.admin.domain.entity.AtricleModel;
import com.ruoyi.admin.domain.entity.SchedulingModel;
import com.ruoyi.admin.domain.entity.SupplierModel;
import com.ruoyi.admin.domain.vo.request.scheduling.*;
import com.ruoyi.admin.domain.vo.response.scheduling.*;
import com.ruoyi.admin.mapper.AtricleMapper;
import com.ruoyi.admin.mapper.SupplierMapper;
import com.ruoyi.admin.service.SchedulingService;
import com.ruoyi.common.api.Result;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 排单(Scheduling)Controller类
 *
 * @author jianhan
 * @date 2022-12-07 15:41:40
 */
@Api(value = "后台排单 Controller", tags = {"后台排单"})
@RestController
@RequestMapping("/api/scheduling")
public class SchedulingController {

    @Autowired
    private SchedulingService schedulingService;

    @Resource
    private AtricleMapper atricleMapper;

    @Resource
    private SupplierMapper supplierMapper;

    /**
     * 根据id获取数据
     *
     * @author jianhan
     * @date 2022-12-07 15:41:40
     **/
    @ApiOperation(value = "根据id获取数据", notes = "根据id获取数据")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "path", dataType = "Long")
    @GetMapping("/{id}")
    public Result<SchedulingPageJoinResponse> get(@PathVariable Long id) {
        return schedulingService.getId(id);
    }


    /**
     * 分页获取排单列表
     *
     * @author jianhan
     * @date 2022-12-07 15:41:40
     **/
    @ApiOperation(value = "分页获取排单列表", notes = "分页获取排单列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "查询页码", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "查询数量", paramType = "query", dataType = "Integer")
    })
    @GetMapping
    public Result<SchedulingPageResponse> pageList(@Valid SchedulingPageJoinRequest req) {
        return schedulingService.page(new Page<>(req.getPageNum(), req.getPageSize()), req);
    }


    /**
     * 添加排单
     *
     * @author jianhan
     * @date 2022-12-07 15:41:40
     **/
    @ApiOperation(value = "添加排单", notes = "添加排单")
    @PostMapping
    public Result add(@RequestBody @Valid SchedulingAddRequest req) {
        return schedulingService.add(req);
    }

    /**
     * 导出排单
     * @author jianhan
     * @return
     */
    @ApiOperation(value = "导出排单", notes = "导出排单")
    @GetMapping("/export")
    public Result export() {
        List<SchedulingModel> list = schedulingService.list();
        if (StringUtils.isEmpty(list)) {
            return Result.failed("无数据");
        }
        List<SchedulingExcel> schedulingExcelList = new ArrayList<SchedulingExcel>();
        for (SchedulingModel schedulingModel : list) {
            SchedulingExcel schedulingExcel = new SchedulingExcel();
            BeanUtils.copyProperties(schedulingModel,schedulingExcel);
            AtricleModel atricleModel = atricleMapper.selectById(schedulingModel.getArticleId());
            schedulingExcel.setArticle(atricleModel.getArticleNumber());
            SupplierModel supplierModel = supplierMapper.selectById(schedulingModel.getSupplierId());
            schedulingExcel.setSupplier(supplierModel.getName());
            schedulingExcelList.add(schedulingExcel);
        }
        ExcelUtil<SchedulingExcel> util = new ExcelUtil<SchedulingExcel>(SchedulingExcel.class);
        return util.exportExcel1(schedulingExcelList, "排单表");
    }

    /**
     * 修改排单
     *
     * @author jianhan
     * @date 2022-12-07 15:41:40
     **/
    @ApiOperation(value = "修改排单", notes = "修改排单")
    @PutMapping()
    public Result update(@RequestBody @Valid SchedulingAddRequest req) {
        return schedulingService.update(req);
    }

    /**
     * @description:  本月上月的毛利
     * @param:
     * @return: profitMonthResponse
     * @author: jianhan
     * @date: 2023/2/9 15:00
     **/
    @ApiOperation(value = "本月上月的毛利", notes = "本月上月的毛利")
    @GetMapping("/getMonthGrossProfit")
    public Result<ProfitMonthResponse> getMonthGrossProfit() {
        return schedulingService.getMonthGrossProfit();
    }

    /**
     * @description:  采购与销售额
     * @param:
     * @return: salesQuotaResponse
     * @author: jianhan
     * @date: 2023/2/9 15:00
     **/
    @ApiOperation(value = "采购与销售额", notes = "采购与销售额")
    @GetMapping("/getQuota")
    public Result<SalesQuotaResponse> getQuota() {
        return schedulingService.getQuota();
    }
    /**
     * @description: 本周客户名称与销售额
     * @param:
     * @return:
     * @author: jianhan
     * @date: 2023/2/12 17:04
     **/
    @ApiOperation(value = "本周客户名称与销售额", notes = "本周客户名称与销售额")
    @GetMapping("/getSupplierQuota")
    public Result<List<SupplierQuotaResponse>> getSupplierQuota() {
        return schedulingService.getSupplierQuota();
    }

    /**
     * @description: 热销榜
     * @param:
     * @return:
     * @author: jianhan
     * @date: 2023/2/13 14:42
     **/
    @ApiOperation(value = "热销榜", notes = "热销榜")
    @GetMapping("/getHotList")
    public Result<List<HotListResponse>> getHotList() {
        return schedulingService.getHotList();
    }

    /**
     * @description: 待处理数量与比例
     * @param:
     * @return:
     * @author: jianhan
     * @date: 2023/2/13 14:56
     **/
    @ApiOperation(value = "待处理数量与比例", notes = "待处理数量与比例")
    @GetMapping("/getPending")
    public Result<PendingResponse> getPending() {
        return schedulingService.getPending();
    }

    /**
     * @description: 每天总数
     * @param:
     * @return:
     * @author: jianhan
     * @date: 2023/2/14 11:31
     **/
    @ApiOperation(value = "每天总数", notes = "每天总数")
    @GetMapping("/getDayTotal")
    public Result<List<DayTotalResponse>> getDayTotal() {
        return schedulingService.getDayTotal();
    }

    /**
     * 修改排单
     *
     * @author jianhan
     * @date 2022-12-07 15:41:40
     **/
    @ApiOperation(value = "修改排单状态", notes = "修改排单状态")
    @PostMapping("/updateByStatus")
    public Result updateByStatus(@RequestBody SchedulingRequest req) {
        return schedulingService.updateByStatus(req);
    }


    /**
     * 删除排单
     *
     * @author jianhan
     * @date 2022-12-07 15:41:40
     **/
    @ApiOperation(value = "删除排单", notes = "删除排单")
    @ApiImplicitParam(name = "ids", value = "id集合,逗号隔开", required = true, paramType = "query", dataType = "String")
    @DeleteMapping("/{ids}")
    public Result update(@PathVariable String ids) {
        Boolean result = schedulingService.removeByIds(Arrays.asList(ids.split(",")).stream().collect(Collectors.toList()));
        return Result.judge(result);
    }

}
